package com.syq.face.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.syq.face.MainActivity;
import com.syq.face.MyApplication;
import com.syq.face.R;
import com.syq.face.StartActivity;
import com.syq.face.model.JsonResult;
import com.syq.face.model.UserInfo;
import com.syq.face.utils.RetrofitUtil;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyFragment extends NotifiedFragment {

    public static final String TAG = "MyFragment";
    private static final String ARG_PARAM1 = "param1";
    private TextView account_info_text;
    private TextView logout_text;
    private UserInfo userInfo;

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    private String mParam1;
    private String mParam2;
    private String username;

    private TextView login_name;


    public void setUsername(String username) {
        this.username = username;
    }

    public MyFragment() {

    }


    public static MyFragment newInstance(String param1, String username) {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setUsername(username);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_my, container, false);
        init(rootView);

        return rootView;
    }

    private void init(View rootView) {
        login_name = rootView.findViewById(R.id.my_index_login_text);
        login_name.setText(userInfo.getUsername());
        account_info_text = rootView.findViewById(R.id.my_index_item_info);
        account_info_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AccountInfoFragment accountInfoFragment = AccountInfoFragment.newInstance(userInfo);
                MyApplication.getRootFragment().start(accountInfoFragment);
            }
        });
        logout_text = rootView.findViewById(R.id.my_index_item_logout);
        logout_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                logout();
            }
        });
    }

    private void logout() {
        RetrofitUtil.getInstance().getUserService().loginOut(MyApplication.getMyself().getToken())
                .enqueue(new Callback<JsonResult>() {
                    @Override
                    public void onResponse(Call<JsonResult> call, Response<JsonResult> response) {
                        JsonResult body = response.body();
                        Integer code = body.getCode();
                        if (code==200){
                            Intent intent = new Intent(getActivity(), StartActivity.class);
                            startActivity(intent);
                            getActivity().finish();
                        }
                    }

                    @Override
                    public void onFailure(Call<JsonResult> call, Throwable t) {

                    }
                });
    }


    @Override
    public void notifyDataReload() {
        RetrofitUtil.getInstance().getUserService().getUser(MyApplication.getMyself().getToken())
                .enqueue(new Callback<JsonResult<UserInfo>>() {
                    @Override
                    public void onResponse(Call<JsonResult<UserInfo>> call, Response<JsonResult<UserInfo>> response) {
                        JsonResult<UserInfo> body = response.body();
                        Log.d(TAG, "onResponse: body:"+new Gson().toJson(body));
                        UserInfo data = body.getData();

                        Log.d(TAG, "onResponse: userinfo"+data);
                        setUserInfo(data);

                    }

                    @Override
                    public void onFailure(Call<JsonResult<UserInfo>> call, Throwable t) {
                        Log.d(TAG, "onFailure: "+t.getMessage());
                        if (t instanceof Exception) {
                            Log.d(TAG, "onFailure: "+t.getMessage()+"code:"+t.getCause());

                        }
                    }
                });

    }
}
